#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct 
{
   char name[20];
   int age;
}stu;
/*int cmp_stu_age(const void* e1,const void*e2)
{
     return ((stu*)e1)->age-((stu*)e2)->age;
}


int  cpm_int(const void* e1,const void* e2)
{
        return *(int*)e2-*(int*)e1;
}

int main()
{
	int arr[10]={1,2,3,54,65,3,65,78,98,65};
    int sz=sizeof(arr)/sizeof(arr[0]);
	qsort(arr,sz,sizeof arr[0],cpm_int);
    for(int i=0;i<sz;i++)
	{
	   printf("%d\t",arr[i]);
	}
   return 0;
}
int main()
{
	stu arr[3]={{"zhangsan",18},{"lisi",20},{"wangwu",19}};
	int sz=sizeof(arr)/sizeof arr[0];
	qsort(arr,sz,sizeof(arr[0]),cmp_stu_age);
	for(int i=0;i<3;i++)
	{
	   printf("%s %d\n",arr[i].name,arr[i].age);

	}
   return 0;
}

int cmp_stu_name(const void* e1,const void* e2)
{
   return strcmp(((stu*)e1)->name,((stu*)e2)->name);
  
}
int main()
{
	stu arr[3]={{"zhangsan",18},{"lisi",20},{"wangwu",19}};
	int sz=sizeof(arr)/sizeof arr[0];
	qsort(arr,sz,sizeof(arr[0]),cmp_stu_name);
	for(int i=0;i<3;i++)
	{
	   printf("%s %d\n",arr[i].name,arr[i].age);

	}
   return 0;
}*/


void swap(char* a,char* b,int width)
{
   for(int i=0;i<width;i++)
   {    char temp=*a;
	   *a=*b;
	   *b=temp;
	   a++;
	   b++;
   }
}

void  bubble_sort(void* base,int size,int width,int (*cmp)(void* e1,void* e2))
{
    for(int i=0;i<size-1;i++)
	{
	   for(int j=0;j<size-i-1;j++)
	   {
	       if(cmp((char*)base+width*j,(char*)base+(j+1)*base)>0)
		   {
		       swap((char*)base+width*j,(char*)base+(j+1)*base),width);
		   }
	   }
	}

}
void test1()
{
   int arr[10]={1,2,3,54,65,3,65,78,98,65};
    int sz=sizeof(arr)/sizeof(arr[0]);
	bubble_sort(arr,sz,sizeof(arr[0]),cmp_int);

}

void test2()
{
    stu arr[3]={{"zhangsan",18},{"lisi",20},{"wangwu",19}};
	int sz=sizeof(arr)/sizeof arr[0];
    bubble_sort(arr,sz,sizeof(arr[0]),cmp_stu_age);
}


int main()
{
    
  test1();
  test2();

   return 0;
}